package com.david.func_test.view

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import com.david.func_test.R

class CustomRatingBar(context: Context, attr: AttributeSet) : View(context, attr) {

    private val selectBitmap: Bitmap =
        BitmapFactory.decodeResource(context.resources, R.mipmap.community_ic_rating_selected)
    private val defaultBitmap: Bitmap =
        BitmapFactory.decodeResource(context.resources, R.mipmap.community_ic_rating_unselect)

    private var ratingSize = 5

    private var mMaxProgress = 100f
    private var mProgress = 10f

    private var itemHeight = 0

    private var ratingRects = ArrayList<RatingBean>()

    private var paint = Paint()

//    init {
//    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
//        itemWidth = (width - paddingLeft - paddingRight)/5
        ratingRects.clear()

        itemHeight = (measuredHeight - paddingTop - paddingBottom)

        val allWidth = measuredWidth - paddingLeft - paddingRight
        val distance = (allWidth - 5 * itemHeight) / 4
        for (i in 0 until ratingSize) {
            ratingRects.add(
                RatingBean(
                    left = paddingLeft + (itemHeight + distance) * i,
                    top = paddingTop,
                    right = paddingLeft + (itemHeight + distance) * i + itemHeight,
                    bottom = paddingBottom
                )
            )
        }

        setRatingProgress(10f)
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        when (event?.action) {
            MotionEvent.ACTION_DOWN -> {
                checkProgress(event.getX(0))
            }
            MotionEvent.ACTION_MOVE -> {
                checkProgress(event.getX(0))
            }
            MotionEvent.ACTION_UP -> {
                checkProgress(event.getX(0))
            }
            MotionEvent.ACTION_CANCEL -> {}
        }
        return true
    }

    private var tempProgress = 0f
    private fun checkProgress(pointX: Float) {
        tempProgress = 0f
        for ((index, item) in ratingRects.withIndex()) {
            if (item.left < pointX && item.right > pointX) {
                if (index > 0) {
                    tempProgress = (index + 1) * mMaxProgress / ratingSize
                }
                tempProgress += (pointX - item.left)/itemHeight * mMaxProgress / ratingSize
                setRatingProgress(tempProgress)
                break
            }
        }
    }

    private var tempBitmapRect = Rect(0, 0, defaultBitmap.width, defaultBitmap.height)
    private var tempCanvasRect = Rect(0, paddingTop, 0, paddingBottom)
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        tempCanvasRect.bottom = itemHeight + paddingTop
        tempBitmapRect.left = 0
        tempBitmapRect.right = defaultBitmap.width
        for (item in ratingRects) {
            if (item.itemProgress < 1) {
                tempBitmapRect.left = 0
                tempBitmapRect.right = defaultBitmap.width
                tempCanvasRect.left = item.left
                tempCanvasRect.right = item.right
                canvas.drawBitmap(defaultBitmap, tempBitmapRect, tempCanvasRect, paint)
            }

            if (item.itemProgress > 0) {
                tempBitmapRect.left = 0
                tempBitmapRect.right = (defaultBitmap.width * item.itemProgress).toInt()
                tempCanvasRect.left = item.left
                tempCanvasRect.right = ((itemHeight * item.itemProgress) + item.left).toInt()
                canvas.drawBitmap(selectBitmap, tempBitmapRect, tempCanvasRect, paint)
            }
        }
    }

    fun setRatingProgress(process: Float) {
        mProgress = process

        val ratingNum = mProgress / mMaxProgress * ratingSize
        var fullStarNum = 1
        if (ratingNum > 1) {
            fullStarNum = ratingNum.toInt()
            for (i in 0 until fullStarNum) {
                ratingRects[i].itemProgress = 1f
            }
        }
        if (fullStarNum > 1)
            ratingRects[fullStarNum - 1].itemProgress = ratingNum - fullStarNum
        else {
            ratingRects[0].itemProgress = ratingNum
        }

        postInvalidate()
    }
}

data class RatingBean(
    var left: Int,
    var top: Int,
    var right: Int,
    var bottom: Int,
    var itemProgress: Float = 0f//是否绘制完整星 0-1
)